#include "ChessmanSprite.h"
#include <tcResource/tc_resource.h>
#include <tcDatabase/tc_chessboard.h>
#include <tcPhysics/tc_physics.h>
#include <tcInput/tc_input.h>
#include <tcLogic/tc_logic.h>


void GunsightSprite::initGunsight()
{
	char path[128];

	tc_resource_get_path(TC_IMG_GUNSIGHT, path);
	this->initWithFile(path, CCRectMake(0, 0, 90, 100));

	mPowerupSprite.initWithFile(path, CCRectMake(90, 0, 90, 100));
	mPowerupSprite.setAnchorPoint(CCPointZero);
	mPowerupSprite.setPosition(CCPointZero);
	mPowerupSprite.setOpacity(0);
	this->addChild(&mPowerupSprite);

	this->setIsVisible(false);
}

void GunsightSprite::setPowerupMode( bool mode )
{
	this->setOpacity(mode ? 0 : 255);
	mPowerupSprite.setOpacity(mode ? 255 : 0);
}

ChessmanSprite::ChessmanSprite() {}
ChessmanSprite::~ChessmanSprite() {}

void ChessmanSprite::init(int id, tcType image) 
{
	mChessmanID = id;

	char path[128];

	tc_resource_get_path(TC_IMG_RED, path);
	CCImage redImage;
	redImage.initWithImageFile(path);
	mRedTexture.initWithImage(&redImage);
	
	tc_resource_get_path(TC_IMG_GREEN, path);
	CCImage greenImage;
	greenImage.initWithImageFile(path);
	mGreenTexture.initWithImage(&greenImage);

	this->initWithTexture(&mRedTexture);

	tc_resource_get_path(image, path);
	mImageSprite.initWithFile(path);
	mImageSprite.setAnchorPoint(ccp(0, 0));
	mImageSprite.setPosition(ccp(0, 0));
	this->addChild(&mImageSprite, 1);

	mGunsightSprite.initGunsight();
	mGunsightSprite.setPowerupMode(false);

	mDeadAnimationDone = false;
	tc_vector2 initPos;
	tc_chessman_get_position(mChessmanID, &initPos);
	mInitPosition.x = initPos.x;
	mInitPosition.y = initPos.y;
}

void ChessmanSprite::reset()
{
	mDeadAnimationDone = false;

	if(mImageSprite.getOpacity() == 0) {
		this->runAction(CCFadeIn::actionWithDuration(0.3f));
		mImageSprite.runAction(CCFadeIn::actionWithDuration(0.3f));
	}
	this->runAction(CCRotateTo::actionWithDuration(0.3f, 0));
	this->runAction(CCMoveTo::actionWithDuration(0.8f, mInitPosition));
}

void ChessmanSprite::update()
{
	this->updatePosition();
	this->updateDead();
	if (!tc_chessman_get_dead(mChessmanID))
	{
		this->updateOwner();
		this->updateSize();
		this->updateGunsight();
	}
}

void ChessmanSprite::updateDead()
{
	BOOL dead = tc_chessman_get_dead(mChessmanID);

	if (dead && !mDeadAnimationDone)
	{
		this->runAction(CCFadeOut::actionWithDuration(0.3f));
		mImageSprite.runAction(CCFadeOut::actionWithDuration(0.3f));
		mGunsightSprite.setIsVisible(false);
		mDeadAnimationDone = true;
	}
}

void ChessmanSprite::updateOwner()
{
	tcType owner = tc_chessman_get_owner(mChessmanID);
	if (owner == TC_PLAYER_ONE) {
		this->setTexture(&mRedTexture);
	}
	else {
		this->setTexture(&mGreenTexture);
	}
}

void ChessmanSprite::updatePosition()
{
	tc_vector2 position;
	tc_chessman_get_position(mChessmanID, &position);
	this->setPosition(ccp(position.x, position.y));

	float rotation;
	rotation = tc_chessman_get_rotation(mChessmanID);
	rotation += mChessmanID < 16 ? 0 : 180;
	this->setRotation(rotation);
	mGunsightSprite.setPosition(ccp(position.x, position.y));
}

void ChessmanSprite::updateSize()
{
	float radius = tc_chessman_get_radius(mChessmanID);
	float scale = radius / 26.0f;
	this->setScale(scale);
}

void ChessmanSprite::updateGunsight()
{
	int targetID = tc_logic_get_selected_chessman();
	mGunsightSprite.setPowerupMode(tc_logic_get_game_state() == TC_GS_USING_POWERUP_PROP);
	if (targetID == mChessmanID)
	{
		this->setOpacity(155);
		float dist = tc_input_get_target_dist();
		float scale = dist < TC_TARGET_MIN_DIST ? 0 : dist/TC_TARGET_MAX_DIST;
		float angle = tc_input_get_target_angle();
		mGunsightSprite.setScale(scale);
		mGunsightSprite.setRotation(angle);
		mGunsightSprite.setIsVisible(true);
	}
	else
	{
		mGunsightSprite.setScale(0);
		mGunsightSprite.setIsVisible(false);
		this->setOpacity(255);
	}
}

CCSprite* ChessmanSprite::getGunsightSprite()
{
	return &mGunsightSprite;
}
